package com.itheima.upload2;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/*
上传图片的 服务器
1.接收客户端上传的图片
2.保存到服务器的本地
3.给客户端 响应一个上传成功

只能接收一次   接受完就下班了

 */
public class Server {
    public static void main(String[] args) throws IOException {
        //2.保存到服务器的本地
        //1.接收客户端上传的图片
        ServerSocket ss = new ServerSocket(8181);


        while (true) {
            Socket accept = ss.accept();

            InputStream is = accept.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day16\\copy\\meinv.jpg"));
            byte[] bytes = new byte[1024];
            int  len;
            while ((len=bis.read(bytes))!=-1){
                //bytes  0, len
                bos.write(bytes,0,len);
            }

            //3.给客户端 响应一个上传成功
            //字节流
            OutputStream os= accept.getOutputStream();
            //不能写汉字
            // OutputStreamWriter writer = ;
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
            bw.write("上传成功!");
            // accept.shutdownOutput();
            bw.flush();
            //释放资源的代码
            bw.close();
            bos.close();
        }
        // ss.close();


    }



}
